package com.nanhai.buddhist.college.module.study.service.qrcodeconfig;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.mybatis.core.dataobject.BaseDO;
import com.nanhai.buddhist.college.module.study.controller.admin.qrcodeconfig.vo.QrCodeConfigCreateReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.qrcodeconfig.vo.QrCodeConfigUpdateReqVO;
import com.nanhai.buddhist.college.module.study.convert.qrcodeconfig.QrCodeConfigConvert;
import com.nanhai.buddhist.college.module.study.convert.studyadditionalcourses.StudyAdditionalCoursesConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.qrcodeconfig.QrCodeConfigDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesDO;
import com.nanhai.buddhist.college.module.study.dal.mysql.qrcodeconfig.QrCodeConfigMapper;
import jodd.util.StringUtil;
import org.codehaus.groovy.classgen.asm.LambdaWriter;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.ADDITIONAL_COURSES_NOT_EXISTS;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.QR_CODE_CONFIG_NOT_EXISTS;

/**
 * @author AKai
 * @date 2022/9/13
 */
@Service
@Validated
public class QrCodeConfigServiceImpl  implements QrCodeConfigService{
    @Resource
    private QrCodeConfigMapper qrCodeConfigMapper;

    @Override
    public String createQrCodeConfig(QrCodeConfigCreateReqVO createReqVO) {
        // 插入
        QrCodeConfigDO qrCodeConfigDO = QrCodeConfigConvert.INSTANCE.convert(createReqVO);
        qrCodeConfigMapper.insert(qrCodeConfigDO);
        return qrCodeConfigDO.getId();
    }

    @Override
    public void updateQrCodeConfig(QrCodeConfigUpdateReqVO updateReqVO) {
        this.validateQrCodeConfigExists(updateReqVO.getId());
        QrCodeConfigDO qrCodeConfigDO = QrCodeConfigConvert.INSTANCE.convert(updateReqVO);
        qrCodeConfigMapper.updateById(qrCodeConfigDO);
    }

    @Override
    public void deleteQrCodeConfig(String id) {
        this.validateQrCodeConfigExists(id);
        qrCodeConfigMapper.deleteById(id);
    }

    @Override
    public QrCodeConfigDO getAQrCodeConfig(String id) {
        if (StringUtil.isBlank(id)) {
            return qrCodeConfigMapper.selectOne(new LambdaQueryWrapper<QrCodeConfigDO>().orderByDesc(BaseDO::getCreateTime).last("limit 1"));
        }else {
            this.validateQrCodeConfigExists(id);
            return qrCodeConfigMapper.selectById(id);
        }

    }

    private void validateQrCodeConfigExists(String id) {
        if (qrCodeConfigMapper.selectById(id) == null) {
            throw exception(QR_CODE_CONFIG_NOT_EXISTS);
        }
    }
}
